package com.example.mybatisplusproject.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplusproject.assemble.UserAssembler;
import com.example.mybatisplusproject.dao.UserDAO;
import com.example.mybatisplusproject.dto.UserAddDTO;
import com.example.mybatisplusproject.dto.UserUpdateDTO;
import com.example.mybatisplusproject.entity.User;
import com.example.mybatisplusproject.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.MessageSource;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @author zls
 * @date 2021/7/10 3:53 下午
 */

@RestController
@RequestMapping("/test")
@Slf4j
@Api(description = "用户相关")
public class TestController {

    @Resource // byname  autowire bytype
    UserService userService;
    @Resource
    UserDAO userDAO;
    @Resource
    MessageSource messageSource;


    @GetMapping("/testabca")
    public String testabca(@RequestParam("BAUD") String baud) {
        return baud;
    }


    @GetMapping("test0")
    
    public String test0() {
        return userService.test0();
    }

    @GetMapping("test1/{id}")
    public String test1(@PathVariable("id") String id) {
        return userService.test1(id);
    }
    @GetMapping("test2/{id}/{name}")
    public String test2(@PathVariable("id") String id, @PathVariable("name") String name) {
        return userService.test2(id, name);
    }
    @GetMapping("test3")
    public String test3() {
        return userService.test3();
    }

    @GetMapping("test4/{id}")
    public String test4(@PathVariable("id") String id) {
        return userService.test4(id);
    }
    @GetMapping("test5/{id}/{name}")
    public String test5(@PathVariable("id") String id, @PathVariable("name") String name) {
        return userService.test5(id, name);
    }

    /**
     * 分页查询  - service
     */
    @ApiOperation("service分页查询")
    @ApiImplicitParams({
            // Valid values are path, query, body, header or form.
            // query、form域中的值需要使用@RequestParam获取， header域中的值需要使用@RequestHeader来获取，path域中的值需要使用@PathVariable来获取，body域中的值使用@RequestBody来获取
            // paramType表示查询类型，query表示get请求后面的拼接，应该与@RequestParam搭配使用
            // path与@PathVariable搭配使用
            // body与@RequestBody搭配使用，见下面addUser方法
            //
            @ApiImplicitParam(name = "pageNum",value = "页数",required = true,paramType = "query"),
            @ApiImplicitParam(name = "pageSize",value = "大小",required = true,paramType = "query")
    })
    @GetMapping("/selectByPage")
    public Page<User> selectByPage(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        return userService.page(new Page<>(pageNum, pageSize));
    }

    /**
     * 分页查询  - mapper
     */
    @GetMapping("/selectByPageMapper")
    public Page<User> selectByPageMapper(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        // 第三个条件若为false，表示：只查一次limit；若不传或传true，表示查一次count(*)无limit + 查一次 limit
        return userDAO.selectPage(new Page<>(pageNum, pageSize,false),null);
    }
    /**
     * 测试service保存是否会回填id
     * @param dto
     * @return
     */
    @PostMapping("/add")
    /*
    这里的dataType表示接收参数的数据类型
     */
    @ApiImplicitParam(name = "UserAddDTO",value = "添加用户DTO",dataType = "UserAddDTO",paramType = "body",required = true)
    public String addUser(@Valid @RequestBody UserAddDTO dto) {
        User user = UserAssembler.toUser(dto);
        userService.save(user);
        // save方法会回填id
        log.info("user: {}",user.getId());
        return "success";
    }

    /**
     * 测试mapper保存是否会回填id
     * @param dto
     * @return
     */
    @PostMapping("/addByMapper")
    public String addUserByMapper(@Valid @RequestBody UserAddDTO dto) {
        User user = UserAssembler.toUser(dto);
        userService.saveByMapper(user);
        log.info("user: {}",user.getId());
        return "success";
    }
    /**
     * 测试service   update时自动填充
     */
    @PutMapping("/update")
    public String update(@Valid @RequestBody UserUpdateDTO dto) {
        userService.updateById(UserAssembler.toUser(dto));
        return "success";
    }
    /**
     * 测试 mapper  update时自动填充
     */
    @PutMapping("/updateByMapper")
    public String updateByMapper(@Valid @RequestBody UserUpdateDTO dto) {
        userDAO.updateById(UserAssembler.toUser(dto));
        return "success";
    }

    /**
     * 测试service 逻辑删除
     */
    @DeleteMapping("/delete/{id}")
    public String deleteById(@PathVariable("id") Long id) {
        userService.removeById(id);
        log.info("逻辑删除后查询is_delete：{}",userService.getById(id).getIsDelete());
        return "success";
    }

    /**
     * 测试mapper 逻辑删除
     */
    @DeleteMapping("/deleteByMapper/{id}")
    public String deleteByMapper(@PathVariable("id") Long id) {
        userDAO.deleteById(id);
        log.info("逻辑删除后查询is_delete：{}",userService.getById(id).getIsDelete());
        return "success";
    }
}
